package com.vueblog.controller;

import com.alibaba.fastjson.JSONObject;
import com.vueblog.po.Comment;
import com.vueblog.service.CommentService;
import com.vueblog.token.AdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Value("${comment.avatar}")
    private String avatar;

    @Value(("${comment.avatarAdmin}"))
    private String adminAvatar;

    @GetMapping("/getComments")
    public Object getComments(@RequestParam int blogId){
        JSONObject jsonObject= new JSONObject();
        jsonObject.put("comments",commentService.listCommentByBlogId(blogId));
        return jsonObject;
    }

    @PostMapping("/saveComment")
    public Object saveComment(@RequestBody Comment comment){
        JSONObject jsonObject= new JSONObject();
        System.out.println(comment);
        //判断是否是管理员
        if (comment.isAdminComment()){
            comment.setAvatar(adminAvatar);
        } else {
            comment.setAvatar(avatar);
        }
        int i = commentService.saveComment(comment);
        if (i>0) {
            jsonObject.put("status", 200);
        }else {
            jsonObject.put("status",500);
            jsonObject.put("msg","发生未知错误");
        }
        return jsonObject;
    }

    @AdminToken
    @GetMapping("/deleteComment")
    public Object deleteComment(@RequestParam int commentId){
        JSONObject jsonObject= new JSONObject();
        int i = commentService.deleteComment(commentId);
        if (i>0) {
            jsonObject.put("status", 200);
        }else {
            jsonObject.put("status",500);
            jsonObject.put("msg","发生未知错误");
        }
        return jsonObject;
    }


}
